use "${datadir}\landreg_matched_big_post_2012", replace
bysort unit_id (dated): gen nextyear = year[_n+1]
bysort unit_id (dated): gen nextdatem = datem[_n+1]
bysort unit_id (dated): gen deltaprice = log(price[_n+1]/price)
gen htb = _merge == 3
egen la = group(localauthoritycode)



*=== Adding staircasing 
merge m:1 schemeid using ///
	"0 - Shared Appreciation\data\HCA_redemptions_all", keep(1 3) ///
	keepusing(Datepurchased HCACash OriginalHCAshare Transactiontype ///
	Completion merge_htb_main) gen(merge_repaid)

gen stair =  mi(nextyear) & regexm(Transaction, "staircasing")

replace nextyear = year(Completion) if stair 
replace nextdatem = mofd(Completion) if stair
replace deltaprice = log(HCACash/OriginalHCAshare) if stair

egen yearnext = group(year nextyear)
egen yearnextla = group(yearnext la)

replace htb = 0 if stair


*--- Regressions
eststo clear

reghdfe deltaprice htb stair, absorb(yearnextla) vce(cluster la nextdatem)
gen mysample = e(sample) // keep same sample across regressions

eststo: reghdfe deltaprice htb if mysample & !stair, ///
	absorb(yearnextla) vce(cluster la nextdatem)
	estadd local FE 	"Purchase year X Sale year X LA"
	estadd local cluster "LA, sale month"
estimate store m1

eststo: reghdfe deltaprice stair if mysample & !htb, ///
	absorb(yearnextla) vce(cluster la nextdatem)
	estadd local FE 	"Purchase year X Sale year X LA"
	estadd local cluster "LA, sale month"
estimate store m2

eststo: reghdfe deltaprice htb stair if mysample, ///
	absorb(yearnextla) vce(cluster la nextdatem)
	estadd local FE 	"Purchase year X Sale year X LA"
	estadd local cluster "LA, sale month"
estimate store m3

esttab m1 m2 m3  using "${tabledir}\price_salestair.tex", b(3) ///
	se star(* 0.10 ** 0.05 *** 0.01) ///
	coeflabel(htb "EL sale" stair "EL staircasing") replace ///
	stats(FE cluster r2 N, fmt (0 0  %4.2fc %10.0fc) ///
	labels( `"\textsc{Fixed effects}"' `"\textsc{SE clustering}"') )  ///
	mtitle("$\Delta$ log Price" "$\Delta$ log Price")  

	
*--- Mass at appreciation = 0 (for text)

program perc_zero
	count if `1'
	local `1'tot = r(N)
	count if deltaprice == 0 & `1'
	di r(N)/``1'tot'
end

perc_zero htb
perc_zero stair


program perc_close_zero
	count if `1'
	local `1'tot = r(N)
	count if inrange(deltaprice, -0.05, 0.05) & `1'
	di r(N)/``1'tot'
end

perc_close_zero htb
perc_close_zero stair
